//
//  JKSuspensionViewManager.m
//  JKFloatingBox
//
//  Created by 王冲 on 2019/9/19.
//  Copyright © 2019 王冲. All rights reserved.
//

#import "JKSuspensionViewManager.h"

#define JKSuspension_IPHONE_WIDTH [UIScreen mainScreen].bounds.size.width
#define JKSuspension_IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height
// 获取相对于4.7寸屏幕的宽或高
#define kCurrentWidth(width) (width / 375.f * JKSuspension_IPHONE_WIDTH)
#define kCurrentHeight(height) (height / 667.f * JKSuspension_IPHONE_HEIGHT)
///悬浮框的宽度
#define suspensionBtnWidth kCurrentWidth(53)
///悬浮框的高度
#define suspensionBtnHeight 40
@interface JKSuspensionViewManager ()<JKSuspensionViewDelegate>

@end
@implementation JKSuspensionViewManager

static id instance;
static dispatch_once_t onceToken;
+ (JKSuspensionViewManager *)sharedSuspensionViewManager {
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (JKSuspensionView *)suspensionBtn {
    if (!_suspensionBtn) {
        
        _suspensionBtn = [[JKSuspensionView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - suspensionBtnWidth-kCurrentWidth(10), ([UIScreen mainScreen].bounds.size.height - suspensionBtnWidth)/2.0-80, suspensionBtnWidth, suspensionBtnWidth) color:[UIColor whiteColor] withSpace:kCurrentWidth(10)];
        _suspensionBtn.hidden = YES;
        _suspensionBtn.delegate = self;
    }
    return _suspensionBtn;
}

/** 创建悬浮球 */
- (void)createSuspension {
    
    if (self.isExist) {
        return;
    }
    
    [self.suspensionBtn showSuspension];
    self.isExist = YES;
    
}

/** 显示悬浮球 */
- (void)displaySuspension {
    
    self.suspensionBtn.hidden = NO;
    if (self.suspensionBlock) {
        self.suspensionBlock();
    }
}

//隐藏悬浮球
- (void)hiddenSuspension {
    self.suspensionBtn.hidden = YES;
    if (self.suspensionBlock) {
        self.suspensionBlock();
    }
}

//改变悬浮球的透明度
- (void)setSuspensionAlpha:(CGFloat)suspensionAlpha {
    
    self.suspensionBtn.alpha = suspensionAlpha;
}

- (CGFloat)suspensionAlpha {
    return self.suspensionBtn.alpha;
}

//移除悬浮球
- (void)removeSuspension{
    self.isExist = NO;
    [self.suspensionBtn removeSuspension];
    //销毁单利
    [self destroy];
}

#pragma mark - JKSuspensionViewDelegate 悬浮球的点击事件
- (void)suspensionViewClick:(JKSuspensionView *)suspensionView {
    
    if (self.clickSuspensionBtnBlock) {
        self.clickSuspensionBtnBlock();
    }
}

#pragma mark 销毁单例
- (void)destroy {
    instance = nil;
    onceToken = 0l;
}

@end
